/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.aaron.platform.view;

import com.aaron.platform.dao.PageInfo;
import com.aaron.platform.entity.*;
import com.aaron.platform.exceptions.DomainNotExistException;
import com.aaron.platform.exceptions.MotorInfoExistsException;
import com.aaron.platform.service.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

/**
 * 车辆基本信息管理页面
 * @author Administrator
 */
public class MotorInfoListBean extends DataListBean {

    private static Logger logger = Logger.getLogger(MotorInfoListBean.class);

    /** 车牌号 */
    private String motorNumber;

    /** 区域ID */
    private int domainId;

    /** 驾驶员 */
    private String driverName;

    private IMotorInfoMgr motorInfoMgr;
    
    private IDomainService domainMgr;
    
    private WlbMotorInfo currentMotorInfo;
    
    private List<SelectItem> domainList;
    
    /** 1:view 2:edit 3:delete */
    private int currentOperation = 1;

    public MotorInfoListBean() {
        super();
        this.tableTitle = FacesUtil.getString("content_title_motor_list");

        // 删除、编辑、查看操作
        this.deletable = true;
        this.editable = true;
        this.viewable = true;
        this.multiSelectable = false;

        // 设置分页信息
        pagable = true;
        init();
        
    }

    /**
     * 初始化查询条件
     */
    private void init() {
        motorNumber = "";
        driverName = "";
        domainId = 0;
        currentMotorInfo = null;
        this.data = new ListDataModel();
        pageInfo = new PageInfo<WlbMotorInfo>();
    }

    public void reset() {
        init();
    }

    @Override
    public void queryData() {

        // 设置查询条件
        Properties properties = new Properties();
        if (this.domainId != 0) {
            properties.setProperty(WlbMotorInfo.PROPERTY_DOMAIN_ID, String.valueOf(this.domainId));
        }
        if (!StringUtils.isEmpty(this.driverName)) {
            properties.setProperty(WlbMotorInfo.PROPERTY_DRIVER_NAME, StringUtils.trim(this.driverName));
        }
        if (!StringUtils.isEmpty(this.motorNumber)) {
            properties.setProperty(WlbMotorInfo.PROPERTY_MOTOR_NUMBER, StringUtils.trim(this.motorNumber));
        }
        
        // 查询车辆信息
        pageInfo = motorInfoMgr.findByCondition(properties, pageInfo.getStartIndex(), pageInfo.getPageSize());
        logger.info("Query motorInfo list finished.");
        data.setWrappedData(pageInfo.getItems());
    }

    @Override
    public void initColumnHeaders() {
        headerColumns.add(new ColumnType(ColumnType.COLUMN_TEXT, "motor_number",
                FacesUtil.getString("table_column_motor_number"), false, "15%"));
        headerColumns.add(new ColumnType(ColumnType.COLUMN_TEXT, "motor_style",
                FacesUtil.getString("table_column_motor_style"), false, "15%"));
        headerColumns.add(new ColumnType(ColumnType.COLUMN_TEXT, "motor_domain",
                FacesUtil.getString("table_column_motor_domain"), false, "15%"));
        headerColumns.add(new ColumnType(ColumnType.COLUMN_TEXT, "driver_name",
                FacesUtil.getString("table_column_motor_drivername"), false, "15%"));
        headerColumns.add(new ColumnType(ColumnType.COLUMN_TEXT, "driver_phone",
                FacesUtil.getString("table_column_motor_driverphone"), false, "20%"));
    }

    @Override
    public String getColumnValue() {
        String columnName = getColumnName();
        increaseColumnIndex();

        WlbMotorInfo obj = (WlbMotorInfo) data.getRowData();
        if (columnName.equals("motor_number")) {
             return obj.getMotorNum();
        } else if (columnName.equals("motor_style")) {
            return obj.getMotorStyle();
        } else if (columnName.equals("motor_domain")) {
            return obj.getDomainInfo().getDomainName();
        } else if (columnName.equals("driver_name")) {
            return obj.getDriverName();
        } else if (columnName.equals("driver_phone")) {
            return obj.getDriverPhone();
        } 
        else {
            return null;
        }
    }

    @Override
    public String editData() {
        currentMotorInfo = (WlbMotorInfo) data.getRowData();
        currentOperation = 2;
        return "motorinfo_input";
    }

    @Override
    public String showDetail() {
        currentMotorInfo = (WlbMotorInfo) data.getRowData();
        currentOperation = 1;
        return "motorinfo_input";
    }
    
    @Override
    public String deleteData() {
        currentMotorInfo = (WlbMotorInfo) data.getRowData();
        currentOperation = 3; 
        motorInfoMgr.removeMotorInfo(currentMotorInfo.getMotorId());
        queryData();
        return "motorinfo_search";
    }
    
    /**
     * 录入新车辆信息
     * @return 进入录入页面
     */
    public String createNewMotorInfo() {
        currentMotorInfo = new WlbMotorInfo();
        currentMotorInfo.setDomainInfo(new ItscDomainInfo());
        currentOperation = 3;
        return "motorinfo_input";
    }
    
    public String saveMotorInfo() {
        
        // 判断输入条件
        if (StringUtils.isBlank(currentMotorInfo.getDriverName())) {
            FacesUtil.addErrorMessage(null, "er_motorinfo_driver_name_required");
            return null;
        } else if (StringUtils.isBlank(currentMotorInfo.getMotorNum())) {
            FacesUtil.addErrorMessage(null, "er_motorinfo_motor_number_required");
            return null;
        } else if (StringUtils.isBlank(currentMotorInfo.getDriverPhone())) {
            FacesUtil.addErrorMessage(null, "er_motorinfo_driver_phone_required");
            return null;
        } else if (StringUtils.isBlank(currentMotorInfo.getMotorStyle())) {
            FacesUtil.addErrorMessage(null, "er_motorinfo_motor_style_required");
            return null;
        } 
        
        // 添加区域信息
        ItscDomainInfo domainInfo;
        try {
            domainInfo = domainMgr.findById(currentMotorInfo.getDomainInfo().getDomainId());
            currentMotorInfo.setDomainInfo(domainInfo);
        } catch (DomainNotExistException ex) {
            java.util.logging.Logger.getLogger(MotorInfoListBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        // 添加或更新车辆息
        if (currentOperation == 2) {
            try {
                motorInfoMgr.updateMotorInfo(currentMotorInfo);
            } catch (MotorInfoExistsException ex) {
                 FacesUtil.addErrorMessage(null, "er_motor_info_exists", currentMotorInfo.getMotorNum());
                 return null;
            }
            FacesUtil.addInfoMessage(null, "info_motor_info_update_success", currentMotorInfo.getMotorNum());
        } else if (currentOperation == 3) {
            try {
                motorInfoMgr.addMotorInfo(currentMotorInfo);
            } catch (MotorInfoExistsException ex) {
                 FacesUtil.addErrorMessage(null, "er_motor_info_exists", currentMotorInfo.getMotorNum());
                 return null;
            }
            FacesUtil.addInfoMessage(null, "info_motor_info_add_success", currentMotorInfo.getMotorNum());
        }   
         
        queryData();
        return "motorinfo_search";
    }
    
    /**
     * 返回上一页
     * @return 上一页
     */
    public String toLastPage() {
        return "motorinfo_search";
    }
    
    public String outputAllMotorInfo() { 
        return null;
    }
   
    public int getDomainId() {
        return domainId;
    }

    public void setDomainId(int domainId) {
        this.domainId = domainId;
    }

    public int getCurrentOperation() {
        return currentOperation;
    }
    
    public List<SelectItem> getDomainList() {
        if (this.domainList == null) {
            List<ItscDomainInfo> domainInfos = domainMgr.findAllDomains();
            domainList = new ArrayList<SelectItem>();
            for (ItscDomainInfo domainInfo : domainInfos) {
                domainList.add(new SelectItem(domainInfo.getDomainId(), domainInfo.getDomainName()));
            }
        }
        return domainList;
    }

    public String getDriverName() {
        return driverName;
    }

    public void setDriverName(String driverName) {
        this.driverName = driverName;
    }

    public String getMotorNumber() {
        return motorNumber;
    }

    public void setMotorNumber(String motorNumber) {
        this.motorNumber = motorNumber;
    }

    public void setDomainMgr(IDomainService domainMgr) {
        this.domainMgr = domainMgr;
    }

    public void setMotorInfoMgr(IMotorInfoMgr motorInfoMgr) {
        this.motorInfoMgr = motorInfoMgr;
    }

    public WlbMotorInfo getCurrentMotorInfo() {
        return currentMotorInfo;
    }
    
}
